package com.twb.system.service.impl;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.base.service.impl.BaseService;
import com.twb.core.exception.ServiceException;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.ExcelUtils;
import com.twb.core.util.TwbPageUtil;
import com.twb.fileCenter.config.FileCenterProperties;
import com.twb.fileCenter.entity.dto.ImportExcelDto;
import com.twb.fileCenter.util.UploadFileUtil;
import com.twb.system.entity.importExport.ImportExport;
import com.twb.system.entity.importExport.dto.ExportExcelImportExportDto;
import com.twb.system.entity.importExport.dto.SaveImportResultInfoDto;
import com.twb.system.entity.importExport.dto.SearchImportExportDto;
import com.twb.system.entity.importExport.vo.SaveImportExportVo;
import com.twb.system.entity.importExport.vo.SaveImportResultInfoVo;
import com.twb.system.entity.importExport.vo.SearchImportExportVo;
import com.twb.system.entity.importExport.vo.UpdateImportExportByIdVo;
import com.twb.system.mapper.ImportExportMapper;
import com.twb.system.service.IImportExportService;

/**
 * @ClassName ImportExportService
 * @Description 异步导入导出记录表service
 * 
 * @author DHL
 * @date 2021-08-09 09:39:11
 */
@Service("importExportService")
public class ImportExportService extends BaseService<ImportExportMapper,ImportExport> implements IImportExportService {

	@Autowired
	ImportExportMapper importExportMapper;
	
	@Autowired
	FileCenterProperties fileCenterProperties;
	
	@Override
	public TwbPageInfo<SearchImportExportDto> searchImportExport(SearchImportExportVo vo) {
		TwbPageUtil.startPage(vo);
		List<SearchImportExportDto> list = importExportMapper.searchImportExport(vo);
		return TwbPageUtil.of(list);
	}

	@Override
	public List<SearchImportExportDto> searchImportExportList(SearchImportExportVo vo) {
		List<SearchImportExportDto> list = importExportMapper.searchImportExport(vo);
		return list;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public ImportExport saveImportExport(SaveImportExportVo vo, HttpServletRequest request) {

		ImportExport record = new ImportExport();
		BeanUtils.copyNotNullProperties(vo, record);
		record.setId(this.getUUID());
		this.setCreateInfo(record);
		this.setUpdateInfo(record);
		
		// 导入Excel
		if ("1".equals(vo.getBizType())) {
			try {
				ImportExcelDto importExcelDto = UploadFileUtil.uploadImportExcelFile(fileCenterProperties, request);
				record.setImportName(importExcelDto.getImportName());
				record.setImportPath(importExcelDto.getImportPath());
				record.setImportFilePath(importExcelDto.getImportFilePath());
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				throw new ServiceException("导入文件写入失败");
			}
		}

		int result = importExportMapper.insert(record);
		
		if (result > 0) {
			return record;
		}
		
		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateImportExportById(UpdateImportExportByIdVo vo) {
		ImportExport record = importExportMapper.selectById(vo.getId());
		if (null == record) {
			throw new ServiceException("数据不存在");
		}

		if (!vo.getName().equalsIgnoreCase(record.getName())) {
			ImportExport checkVo = new ImportExport();
			checkVo.setName(vo.getName()); // 此处需要修改为需要校验重复的字段
			checkVo.setIsdel(0);
			long count = this.count(checkVo);
			if (count > 0) {
				throw new ServiceException(String.format("[%s]已存在", checkVo.getName()));
			}
		}

		BeanUtils.copyNotNullProperties(vo, record);
		this.setUpdateInfo(record);
		int result = importExportMapper.updateById(record);
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int deleteImportExportById(String id) {
		int result = importExportMapper.deleteById(id);
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int deleteImportExportByIds(String ids) {
		return importExportMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int logicBatchDeleteImportExportByIds(String ids) {
		List<String> list = new ArrayList<String>();
		Collections.addAll(list, ids.split(","));
		return importExportMapper.logicBatchDeleteByIds(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void exportExcel(SearchImportExportVo vo, HttpServletResponse response) throws Exception {
		List<ExportExcelImportExportDto> list = importExportMapper.searchExportImportExport(vo);
		ExcelUtils.exportExcel("异步导入导出记录表", list, ExportExcelImportExportDto.class, response);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int importExcel(Map<String, String> map) {
		System.out.println(map.toString());
		return 0;
	}
	
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public ImportExport execute(String importExcelId) {
		ImportExport importExport = this.getById(importExcelId);
		
		this.setUpdateInfo(importExport);
		
		importExport.setResult("1");
		importExport.setResultDate(LocalDateTime.now());
		
		importExportMapper.updateById(importExport);
		
		return importExport;
	}
	
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public SaveImportResultInfoDto saveImportResult(SaveImportResultInfoVo vo) throws BeansException, InstantiationException, IllegalAccessException {
		
		vo.setUpdateDate(LocalDateTime.now());
		
		vo.setResult("2");
		vo.setResultDate(LocalDateTime.now());
		
		int result = importExportMapper.saveImportResult(vo);
		if(result > 0) {
			ImportExport importExport = this.getById(vo.getId());
			
			SaveImportResultInfoDto importResultInfoDto = BeanUtils.copyNotNullProperties(importExport, SaveImportResultInfoDto.class);
			
			return importResultInfoDto;
		}
		return null;
	}
}
